/**
 * 
 */
package com.alstudio.ui.module.notice;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.module.xmpp.protocol.xmpp.packact.ALMessage;
import com.alstudio.utils.formater.time.ALTimeUtils;
import com.alstudio.view.absadapter.BaseUserListAdapter.AddOnIconClickListener;
import com.loovee.imaohu.R;
import com.nhaarman.listviewanimations.itemmanipulation.ExpandableListItemAdapter;

/**
 * @author alonso lee
 * 
 */
public class SystemNoticeAdapter2 extends ExpandableListItemAdapter<ALMessage> {

	private final Context mContext;
	private LayoutInflater mLayoutInflater;
	private ArrayList<ALMessage> noticeData = null;
	private boolean showDelete = false;

	private AddOnIconClickListener addOnIconClickListener;

	public SystemNoticeAdapter2(final Context context, LayoutInflater inflater,
			final ArrayList<ALMessage> items) {
		super(context, R.layout.activity_expandablelistitem_card,
				R.id.activity_expandablelistitem_card_title,
				R.id.activity_expandablelistitem_card_content, items);
		this.mLayoutInflater = inflater;
		mContext = context;
		noticeData = items;
	}

	/**
	 * @param showDelete
	 *            the showDelete to set
	 */
	public void setShowDelete(boolean showDelete) {
		this.showDelete = showDelete;
	}

	/**
	 * @return the showDelete
	 */
	public boolean isShowDelete() {
		return showDelete;
	}

	/**
	 * 删除按钮监听器
	 * 
	 * @param deleteListener
	 */
	public void setDeleteClickListener(AddOnIconClickListener deleteListener) {
		addOnIconClickListener = deleteListener;
	}

	public int getRealCount() {
		// TODO Auto-generated method stub
		if (noticeData != null) {
			return noticeData.size();
		}
		return 0;
	}

	@Override
	public View getTitleView(final int position, View convertView,
			final ViewGroup parent) {

		GroupView mGroupView = new GroupView();

		ALMessage msg = noticeData.get(position);

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.system_notice_group_item, null);
		}

		mGroupView.image_icon = (ImageView) convertView
				.findViewById(R.id.image_icon);
		mGroupView.setting_notice_group_title = (TextView) convertView
				.findViewById(R.id.setting_notice_group_title);
		mGroupView.setting_notice_group_time = (TextView) convertView
				.findViewById(R.id.setting_notice_group_time);
		mGroupView.setting_notice_expand_icon = (ImageView) convertView
				.findViewById(R.id.setting_notice_expand_icon);

		mGroupView.delet = (TextView) convertView.findViewById(R.id.delete_bt);

		convertView.setTag(msg);

		mGroupView.setting_notice_group_title.setText("");
		mGroupView.setting_notice_group_time.setText("");

		mGroupView.setting_notice_group_title.setText(msg.getSubject());

		if (msg.getDate() != null && !msg.getDate().equals("null")) {
			mGroupView.setting_notice_group_time.setText(ALTimeUtils
					.getNoticeTime(mContext, msg.getDate()));
		}

		if (showDelete) {
			mGroupView.delet.setVisibility(View.VISIBLE);
			// holder.delet.startAnimation(AnimationUtils.loadAnimation(ALLocalEnv.getLocalEnv(),
			// R.anim.activity_left_to_right));
			mGroupView.setting_notice_expand_icon.setVisibility(View.GONE);
			setupAddOnIconClickEvent(mGroupView.delet, position);
//			mGroupView.delet.startAnimation(AnimationUtils.loadAnimation(
//					ALLocalEnv.getLocalEnv(), R.anim.fade_in));
		} else {
			mGroupView.delet.setVisibility(View.GONE);
			mGroupView.setting_notice_expand_icon.setVisibility(View.VISIBLE);
			mGroupView.delet.setOnClickListener(null);
		}

		return convertView;
	}

	@Override
	public View getContentView(final int position, View convertView,
			ViewGroup parent) {

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.system_notice_child_item, null);
		}
		TextView text = (TextView) convertView.findViewById(R.id.child_webview);

		View button_layout = convertView.findViewById(R.id.button_layout);
		View button1 = convertView.findViewById(R.id.button1);
		View button2 = convertView.findViewById(R.id.button2);
		View left_icon = convertView.findViewById(R.id.left_icon);
		View right_icon = convertView.findViewById(R.id.right_icon);

		TextView left_name = (TextView) convertView
				.findViewById(R.id.left_name);
		TextView right_name = (TextView) convertView
				.findViewById(R.id.right_name);

		text.setText("");
		left_name.setText("");
		right_name.setText("");
		ALMessage msg = noticeData.get(position);

		text.setText(msg.getBody());

		return convertView;
	}

	private void setupAddOnIconClickEvent(View view, Object data) {
		view.setTag(data);
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (addOnIconClickListener != null) {
					addOnIconClickListener.onAddOnIconClick(v.getTag());
				}
			}
		});
	}

	private class GroupView {
		public ImageView image_icon;
		public TextView setting_notice_group_title, delet;
		public TextView setting_notice_group_time;
		public ImageView setting_notice_expand_icon;
	}

}
